import { Singleton } from "db://assets/Scripts/Base/Singleton";

export class CommonUtil extends Singleton {
  switchSetActive(objects: any[], targetObject: any): void {
    objects.forEach((obj) => {
      if (obj === targetObject) {
        obj.active = true;
      } else {
        obj.active = false;
      }
    });
  }

  obtainFromArrayByName(argsArray: any[], attributeName: string) {
    let obj = argsArray.find((obj) => obj._name === attributeName);
    return obj;
  }

  formatCurrentTime(): string {
    const now = new Date();
    const year = now.getFullYear();

    const month =
      (now.getMonth() + 1).toString().length < 2
        ? "0" + (now.getMonth() + 1)
        : now.getMonth() + 1;

    const day =
      now.getDate().toString().length < 2 ? "0" + now.getDate() : now.getDate();

    const hours =
      now.getHours().toString().length < 2
        ? "0" + now.getHours()
        : now.getHours();

    const minutes =
      now.getMinutes().toString().length < 2
        ? "0" + now.getMinutes()
        : now.getMinutes();

    const seconds =
      now.getSeconds().toString().length < 2
        ? "0" + now.getSeconds()
        : now.getSeconds();

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }

  // 重写getInstance方法,确保返回类型为 CommonUtil
  public static getInstance(): CommonUtil {
    if (!this.instance) {
      this.instance = new this();
    }
    return this.instance as CommonUtil;
  }
}
